/*
 * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
 *
 * Author:     fanpengcheng <fanpengcheng_cm@deepin.com>
 *
 * Maintainer: fanpengcheng <fanpengcheng_cm@deepin.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "switchbutton.h"

#include <DIconButton>
#include <QApplication>

#include <QDebug>
#include <QPalette>
#include <QStyleOption>
#include <QTimer>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QResizeEvent>
#include <QVBoxLayout>

DWIDGET_USE_NAMESPACE

#define MAX_BTN_NUM (48)
#define MAX_CNT_ROW (6)

SwitchButton::SwitchButton(QWidget *parent) :
    QWidget(parent),
    m_eShape(e_Rect),
    m_checked(false),
    m_radius(0),
    m_rate(2),
    m_ballPos(QPoint(0, 0)),
    m_timer(nullptr)
{
    initMember();
    initWidget();
}

SwitchButton::~SwitchButton()
{

}

void SwitchButton::initMember()
{
    m_timer = new QTimer(this);
    m_timer->setInterval(1000 / 60);
    connect(m_timer, &QTimer::timeout, this, &SwitchButton::slot_updatePos);
    m_timer->start();
}

void SwitchButton::initWidget()
{
    setAttribute(Qt::WA_TranslucentBackground, true);
}

QSize SwitchButton::sizeHint() const
{
    return QSize(100, 50);
}

QSize SwitchButton::minimumSizeHint() const
{
    return QSize(50, 25);
}

void SwitchButton::setChecked(bool check)
{
    m_checked = check;
    repaint();
}

bool SwitchButton::isChecked() const
{
    return m_checked;
}

void SwitchButton::setShapeType(eShapeType eShape)
{
    m_eShape = eShape;
    repaint();
}

eShapeType SwitchButton::shapeType() const
{
    return m_eShape;
}

void SwitchButton::setRate(int rate)
{
    if (rate < 1 || rate > 10)
        return;
    m_rate = rate;
}

int SwitchButton::rate() const
{
    return m_rate;
}

void SwitchButton::slot_updatePos()
{
    double dbStep = ((width() - (m_radius * 2)) * 1.0) / 20;
    if (m_checked) {
        m_ballPos.setX(m_ballPos.x() + dbStep);
    } else {
        m_ballPos.setX(m_ballPos.x() - dbStep);
    }

    if (m_ballPos.x() < 0) {
        m_ballPos = QPointF(0, 0);
    } else if (m_ballPos.x() > width() - m_radius * 2) {
        m_ballPos = QPointF(width() - m_radius * 2, 0);
    }
    repaint();
}

void SwitchButton::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QPainter painter(this);

    //get p
    QStyleOption opt;
    opt.initFrom(this);
    QPalette p = opt.palette;
    QColor c = p.color(m_checked ? (QPalette::ColorRole::Highlight) : (QPalette::ColorRole::Foreground));

    m_radius = qMin(width(), height()) / 2;

    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    drawBg(&painter, c);
    drawBall(&painter, c);
}

void SwitchButton::mousePressEvent(QMouseEvent *e)
{
    m_checked = !m_checked;
    m_timer->start();
    return QWidget::mousePressEvent(e);
}

void SwitchButton::drawBg(QPainter *painter, QColor c)
{

    painter->save();

    QPen pen(c);
    painter->setPen(pen);

    QRectF rect(0, 0, width(), m_radius * 2);

    switch (m_eShape) {
    case e_Ellipse: {
        painter->drawRoundedRect(rect, m_radius, m_radius);
    }
    break;
    case e_Rect: {
        painter->drawRoundedRect(rect, m_radius / m_rate, m_radius / m_rate);
    }
    break;
    default:
        break;
    }

    painter->restore();
}

void SwitchButton::drawBall(QPainter *painter, QColor c)
{
    painter->save();

    painter->setBrush(c);
    QRectF rect(m_ballPos.x(), m_ballPos.y(), m_radius * 2, m_radius * 2);
    paintShape(painter, rect, m_eShape);

    painter->restore();
}

void SwitchButton::paintShape(QPainter *painter, QRectF rect, eShapeType eShapeType)
{
    switch (eShapeType) {
    case e_Ellipse: {
        painter->drawEllipse(rect);
    }
    break;
    case e_Rect: {
        painter->drawRoundedRect(rect, m_radius / m_rate, m_radius / m_rate);
    }
    break;
    default:
        break;
    }
}
